package com.black.cat.jvm.classfile.attribute;

import com.black.cat.jvm.classfile.ClassReader;

/**
 * @Author blackcat
 * @create 2021/8/16 14:06
 * @version: 1.0
 * @description:
 * BootstrapMethods 属性是一个变长属性，用于保存 invokedynamic 指令引用的引导方法限定符。
 * BootstrapMethods_attribute {
 * u2 attribute_name_index;
 * u4 attribute_length;
 * u2 num_bootstrap_methods;
 * { u2 bootstrap_method_ref;
 * u2 num_bootstrap_arguments;
 * u2 bootstrap_arguments[num_bootstrap_arguments];
 * } bootstrap_methods[num_bootstrap_methods];
 * }
 */
public class BootstrapMethodsAttribute implements AttributeInfo {

    BootstrapMethod[] bootstrapMethods;

    @Override
    public void readInfo(ClassReader reader) {
        int bootstrapMethodNum = reader.nextU2ToInt();
        bootstrapMethods = new BootstrapMethod[bootstrapMethodNum];
        for (int i = 0; i < bootstrapMethodNum; i++) {
            bootstrapMethods[i] = new BootstrapMethod(reader.nextU2ToInt(), reader.nextUint16s());
        }
    }

    static class BootstrapMethod {

        int bootstrapMethodRef;

        int[] bootstrapArguments;

        BootstrapMethod(int bootstrapMethodRef, int[] bootstrapArguments) {
            this.bootstrapMethodRef = bootstrapMethodRef;
            this.bootstrapArguments = bootstrapArguments;
        }
    }
}
